/**
 * 文件拷贝
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <unistd.h>

#include <sys/stat.h>
#include <fcntl.h>

#define BUF_MAX  4096

int main(int argc, char **argv)
{

    if (argc <= 2)
    {
        fprintf(stderr, "Uasge: %s src_file_name dest_file_name\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    int src_fd, dest_fd;
    src_fd = open(argv[1], O_RDONLY);
    dest_fd = open(argv[2], O_CREAT | O_WRONLY, S_IRWXU);

    if (src_fd < 0 || dest_fd < 0)
    {
        fprintf(stderr, "open error!\n");
        exit(EXIT_FAILURE);
    }

    char buf[BUF_MAX];
    int count;
    while (1)
    {
        memset(buf, 0, BUF_MAX);
        count = read(src_fd, buf, sizeof(buf));
        if (count > 0)
        {
            write(dest_fd, buf, count);
            if(count < BUF_MAX){
                break;
            }
        }

        if(count < 0){
            if(errno == EINTR){
                continue;
            }
        }
    }

    close(src_fd);
    close(dest_fd);

    return 0;
}